package org.darcy.youchat.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.darcy.youchat.imageloader.MyImageLoader;
import org.darcy.youchat.utils.AppConst;
import org.darcy.youchat.R;
import org.darcy.youchat.activity.me.card.CardPaketActivity;
import org.darcy.youchat.activity.me.myinfo.MyInfoActivity;
import org.darcy.youchat.activity.me.setting.SettingActivity;
import org.darcy.youchat.databinding.FragmentMeBinding;
import org.darcy.youchat.factory.ThreadPoolFactory;
import org.darcy.youchat.model.UserCache;
import org.darcy.youchat.nimsdk.NimUserInfoSDK;
import org.darcy.youchat.utils.UIUtils;
import org.darcy.youchat.view.CustomDialog;

import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.uinfo.constant.GenderEnum;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * 4.我
 */
public class MeFragment extends BaseFragment {

    private FragmentMeBinding binding;

    private NimUserInfo mNimUserInfo;
    private View mQRCodeCardView;
    private CustomDialog mQRCodeCardDialog;
    private ImageView mIvHeaderQRCodeCard;
    private TextView mTvNameQRCodeCard;
    private ImageView mIvGenderQRCodeCard;
    private ImageView mIvCardQRCodeCard;


    @Override
    public View initView() {
        binding = FragmentMeBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void initData() {
        mNimUserInfo = NimUserInfoSDK.getUser(UserCache.getAccount());
        if (mNimUserInfo == null) {
            getUserInfoFromRemote();
        } else {
            //头像
            if (!TextUtils.isEmpty(mNimUserInfo.getAvatar()) && binding.ivHeader != null) {
                MyImageLoader.LoadNetImage(mNimUserInfo.getAvatar(), binding.ivHeader);
            }

            //用户名、账号
            if (binding != null) {
                if (binding.tvName != null)
                    binding.tvName.setText(mNimUserInfo.getName());
                if (binding.tvAccount != null)
                    binding.tvAccount.setText(mNimUserInfo.getAccount());
            }
        }
    }

    @Override
    public void initListener() {
        //1.个人信息
        binding.llMyInfo.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), MyInfoActivity.class);
            startActivity(intent);
        });
        //2.二维码
        binding.ivQRCordCard.setOnClickListener(v -> {
            showQRCode();
        });
        //3.卡包
        binding.oivCardPacket.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), CardPaketActivity.class);
            startActivity(intent);
        });
        //4.系统设置
        binding.oivSetting.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SettingActivity.class);
            startActivity(intent);
        });
    }

    private void showQRCode() {
        if (mQRCodeCardView == null) {
            mQRCodeCardView = View.inflate(getActivity(), R.layout.include_qrcode_card, null);
            mQRCodeCardView.setBackgroundResource(R.drawable.shape_corner_rect_solid_white);
            mIvHeaderQRCodeCard = mQRCodeCardView.findViewById(R.id.ivHeader);
            mTvNameQRCodeCard = mQRCodeCardView.findViewById(R.id.tvName);
            mIvGenderQRCodeCard = mQRCodeCardView.findViewById(R.id.ivGender);
            mIvCardQRCodeCard = mQRCodeCardView.findViewById(R.id.ivCard);
            mQRCodeCardDialog = new CustomDialog(getActivity(), 300, 400, mQRCodeCardView, R.style.dialog);
        }

        String avatar = mNimUserInfo.getAvatar();
        if (!TextUtils.isEmpty(avatar))
            MyImageLoader.LoadNetImage(avatar, mIvHeaderQRCodeCard);
        else
            mIvHeaderQRCodeCard.setImageResource(R.mipmap.default_header);
        mTvNameQRCodeCard.setText(mNimUserInfo.getName());
        if (mNimUserInfo.getGenderEnum() == GenderEnum.FEMALE) {
            mIvGenderQRCodeCard.setImageResource(R.mipmap.ic_gender_female);
        } else if (mNimUserInfo.getGenderEnum() == GenderEnum.MALE) {
            mIvGenderQRCodeCard.setImageResource(R.mipmap.ic_gender_male);
        } else {
            mIvGenderQRCodeCard.setVisibility(View.GONE);
        }
        showQRCordCard();
        mQRCodeCardDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mQRCodeCardDialog != null)
            mQRCodeCardDialog.dismiss();
    }

    private void getUserInfoFromRemote() {
        List<String> accountList = new ArrayList<>();
        accountList.add(UserCache.getAccount());
        NimUserInfoSDK.getUserInfosFormServer(accountList, new RequestCallback<List<NimUserInfo>>() {
            @Override
            public void onSuccess(List<NimUserInfo> param) {
                initData();
            }

            @Override
            public void onFailed(int code) {
                UIUtils.showToast("获取用户信息失败" + code);
            }

            @Override
            public void onException(Throwable exception) {
                exception.printStackTrace();
            }
        });
    }

    private void showQRCordCard() {
        ThreadPoolFactory.getNormalPool().execute(() -> {
            final Bitmap codeWithLogo5 = QRCodeEncoder.syncEncodeQRCode(AppConst.QRCodeCommend.ACCOUNT + mNimUserInfo.getAccount(), UIUtils.dip2Px(200));
            UIUtils.postTaskSafely(() -> mIvCardQRCodeCard.setImageBitmap(codeWithLogo5));
        });
    }
}
